import { util } from '../../../utils/util';
var app = getApp();
Page({
  data: {
    userName: '',
    IDCard: '',
    phoneNumber: '',
  },
  onSubmit: function(e) {
      let that = this;
      let form = e.detail.value;
      console.log('form发生了submit事件，携带数据为：', e.detail.value);
      //校验
      if(form.userName===''){
        console.warn('请输入姓名');
        util.showError('请输入【姓名】')
      }else if(form.phoneNumber===''){
        console.warn('请输入电话');
        util.showError('请输入【电话】')
      }else if(form.IDCard===''||!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(form.IDCard))){
        console.warn('请输入正确身份证');
        util.showError('请输入正确【身份证】')
      }else{
           wx.request({
                    url: app.LkServerAddress+'/dingtalk/addCard',
                    method: 'POST',
                    data: JSON.stringify({
                        name: form.userName,
                        idCard:form.IDCard,
                        type : 1,
                    }),
                    dataType: 'json',
                    header: {
                       'Authorization' : app.cardToken,
                       'Content-Type'	: 'application/json',
                     },
                    success: function(res) {
                     console.log('success----',res)
                     if(res.data.code==0){
                      if(res.data.info.isSuccess){
                        //是否开卡成功，跳卡列表页面
                        wx.showToast({
                          title: '开卡成功',
                          icon: 'success',
                          duration: 2000,
                          success() {
                              console.log('toast end');
                              wx.reLaunch({
                                url: '../student-toggle-login/index'
                              })
                            },
                        });
                      }else{
                        util.showError(JSON.stringify(res.data.message))
                      }
                    }else if(res.data.code==1006){
                      wx.showToast({
                          title: JSON.stringify(res.data.message),
                          icon: 'none',
                          duration: 2000,
                          success() {
                          },
                      });
                      }else{
                        util.showError(JSON.stringify(res.data.message))
                      }
                      
                    },
                    fail: function(res) {
                      console.log("httpRequestFail---",res)
                      util.showError(JSON.stringify(res.errMsg))
                    },
                    complete: function(res) {
                    }
                    
                }); 
        
      }
      
    },

    onLoad(query) {
    // 页面加载
    this.setData({
          phoneNumber: app.mobile,
          userName: app.username,
          IDCard: app.myCard,
        })

   
    
  },
});
